using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WhaleFall : MonoBehaviour
{
    // Start is called before the first frame update
    Rigidbody2D myrigindbody;
    Collider2D mycollider;
    public float damage;
    Animator animator;
    Dictionary<GameObject,bool> dic=new Dictionary<GameObject, bool>();
    
    void Start()
    {
        animator = GetComponentInParent<Animator>();
        myrigindbody = GetComponent<Rigidbody2D>();
        mycollider = GetComponent<Collider2D>();
        mycollider.enabled = false;
        Vector3 vel = new Vector3(0, 20, 0);
        myrigindbody.velocity = vel;
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        //Debug.Log("GameObject2 collided with " + other.name);
        GameObject name = other.gameObject;
        if (other.gameObject.CompareTag("Player"))
        {

            if (dic.ContainsKey(name) == false)
            {
                dic.Add(name, true);
                other.GetComponent<PlayerBasic>().TakeDamage(damage, 0, 0, 0, 0);
            }
        }
        else if (other.gameObject.CompareTag("Shield"))
        {
            if (dic.ContainsKey(name) ==false)
            {
                dic.Add(name, true);
                other.GetComponent<shield>().TakeDamage(damage);
            }
        }
        else if (other.gameObject.CompareTag("Creation"))
        {
            if (dic.ContainsKey(name) == false)
            {
                dic.Add(name, true);
                other.GetComponent<Creation>().TakeDamage(damage);
            }
        }
        else if (other.gameObject.CompareTag("Enemy"))
        {
            if (dic.ContainsKey(name) ==false){
                dic.Add(name, true);
                other.GetComponent<EnemyBasic>().TakeDamage(damage, 0, 0, 0, 0);
            }

        }
        else if (other.gameObject.CompareTag("GrindingTower"))
        {
            if (dic.ContainsKey(name) ==false)
            {
                dic.Add(name, true);
                other.GetComponent<Grinding>().TakeDamage(damage);
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (myrigindbody.velocity.y < 8)
        {
            mycollider.enabled = true;
            animator.SetBool("down", true);
            
        }
        if (myrigindbody.velocity.y < -30)
        {
            Destroy(gameObject);
        }
    }
}
